export function createDomNode(type, attrs) {
  const obj = document.createElement(type)
  for (const key in attrs) {
    obj.setAttribute(key, attrs[key].toString())
  }
  return obj
}

export function createParamNode(_name, _value) {
  return createDomNode('param', {
    name: _name.toString(),
    value: _value.toString()
  })
}

export function createObjectNode(attrs, params) {
  // 创建object节点
  const objectNode = createDomNode('object', attrs)
  // 隐藏边框
  objectNode.style.borderStyle = 'none'
  // 增添param子节点
  for (const key in params) {
    objectNode.appendChild(createParamNode(key, params[key]))
  }
  return objectNode
}

export function createTransparentIFrame() {
  const iframeNode = createDomNode('iframe', {
    frameborder: '0',
    scrolling: 'no',
    allowTransparency: 'true'
  })
  iframeNode.style.backgroundColor = 'transparent'
  iframeNode.style.position = 'absolute'
  iframeNode.style.zIndex = '-1'
  iframeNode.style.width = '100%'
  iframeNode.style.height = '100%'
  iframeNode.style.left = '0'
  iframeNode.style.top = '0'
  return iframeNode
}

// 判断class有无
export function hasClass(ele, cls) {
  if (ele) {
    cls = cls || ''
    if (cls.replace(/\s/g, '').length === 0) return false // 当cls没有参数时，返回false
    return new RegExp(' ' + cls + ' ').test(' ' + ele.className + ' ')
  }
}

// 添加class
export function addClass(ele, cls) {
  if (!hasClass(ele, cls) && ele) {
    ele.className = ele.className === '' ? cls : ele.className + ' ' + cls
  }
}

// 去除class
export function removeClass(ele, cls) {
  if (hasClass(ele, cls) && ele) {
    var newClass = ' ' + ele.className.replace(/[\t\r\n]/g, '') + ' '
    while (newClass.indexOf(' ' + cls + ' ') >= 0) {
      newClass = newClass.replace(' ' + cls + ' ', ' ')
    }
    ele.className = newClass.replace(/^\s+|\s+$/g, '')
  }
}
